package org.enwey.icarenew.Activity.Personal;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Manager.SettingsManager;
import org.enwey.icarenew.Model.Topic;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;

import java.util.Hashtable;

/**
 * Created by John on 1/26/2015.
 */
public class ShareNewActivity extends BaseActivity {
    private EditText mTitle;
    private EditText mContent;
    private String mNewId;
    private TextView mShare;
    private final int CONTENT_TO_TITLE = 15;
    private boolean isEmpty;
    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        setUp();
        findViews();
        registerListener();
    }

    private void setUp() {
        showLeftBtn();
        intentData();
        isEmpty = true;
        mUser = SettingsManager.getLoginUser();
        if (mUser == null) {
            ToastUtil.show(ShareNewActivity.this, "请登录...");
            finish();
        }
    }

    private void findViews() {
        mTitle = (EditText) this.findViewById(R.id.activity_share_title);
        mContent = (EditText) this.findViewById(R.id.activity_share_content);
        mShare = (TextView) this.findViewById(R.id.header_right_label);
    }

    private void intentData() {
        mNewId = getIntent().getStringExtra("newId");
        if (TextUtils.isEmpty(mNewId)) {
            setTitle("发布新闻");
            showRightLabel("发布");
        } else {
            setTitle("编辑新闻");
            showRightLabel("修改");
        }
    }

    private void doRightLabel() {
        if (isEmpty) {
            return;
        }
        String title = mTitle.getText().toString().trim();
        String content = mContent.getText().toString().trim();

        if (TextUtils.isEmpty(content)) {
            ToastUtil.show(ShareNewActivity.this, "新闻内容不能为空!");
            return;
        }

        if (TextUtils.isEmpty(title) && content.length() > CONTENT_TO_TITLE) {
            title = content.substring(0, CONTENT_TO_TITLE);
        } else {
            title = content.substring(0, content.length());
        }
        if (mUser != null && mUser.id != -1) {
            CallAPIWriteTopic(title, content, mUser.id + "");
        }

    }

    private void CallAPIWriteTopic(String title, String content, String userId) {
        showProgressDialog("发布中...").setCancelable(false);
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("title",title);
        parameters.put("content",content);
        parameters.put("user_id",userId);

        APIManager.getInstance(this).writeTopic(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                hideProgressDialog();
                showException(error);

            }
        }, new Response.Listener<RequestResult<Topic>>() {
            @Override
            public void onResponse(RequestResult<Topic> response) {
                hideProgressDialog();
                if (hasError(response)) {
                    return;
                }
                if (response.code == 200) {
                    ToastUtil.show(ShareNewActivity.this, response.message);
                    finish();
                }

            }
        });
    }

    private void registerListener() {

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    showRightLabelBeLight(true);
                    isEmpty = false;
                } else {
                    showRightLabelBeLight(false);
                    isEmpty = true;
                }
            }
        };

        mContent.addTextChangedListener(watcher);

        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRightLabel();
            }
        };
        mShare.setOnClickListener(register);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mUser = SettingsManager.getLoginUser();
        if (mUser == null) {
            ToastUtil.show(ShareNewActivity.this, "请登录...");
            finish();
        }
    }
}
